package com.tarena.lbs.activity.rpc;

import com.tarena.lbs.activity.api.ActivityApi;
import com.tarena.lbs.activity.api.pojo.ActivityDto;
import com.tarena.lbs.activity.pojo.query.ActivityQuery;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.activity.service.ActivityService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
@DubboService
public class ActivityApiImpl implements ActivityApi {

    @Autowired
    private ActivityService activityService;

    @Override
    public Boolean activityVisible(Integer userId, Integer activityId) {
        log.info("Dubbo API调用activityVisible，用户ID: {}, 活动ID: {}", userId, activityId);
        try {
            return activityService.checkActivityVisible(userId, activityId);
        } catch (BusinessException e) {
            log.error("检查用户是否满足活动目标人群范围失败，userId: {}, activityId: {}, error: {}",
                    userId, activityId, e.getMessage(), e);
            return false;
        } catch (Exception e) {
            log.error("检查用户是否满足活动目标人群范围发生异常，userId: {}, activityId: {}, error: {}",
                    userId, activityId, e.getMessage(), e);
            return false;
        }
    }

    @Override
    public ActivityDto getActivityById(Integer activityId) {
        log.info("Dubbo API调用getActivityById，活动ID: {}", activityId);
        try {
            ActivityVO activityVO = activityService.getActivityById(activityId);
            if (activityVO == null) {
                log.warn("活动不存在，活动ID: {}", activityId);
                return null;
            }

            // 转换VO为DTO
            ActivityDto activityDto = new ActivityDto();
            BeanUtils.copyProperties(activityVO, activityDto);
            return activityDto;
        } catch (BusinessException e) {
            log.error("获取活动详情失败，activityId: {}, error: {}", activityId, e.getMessage(), e);
            return null;
        } catch (Exception e) {
            log.error("获取活动详情发生异常，activityId: {}, error: {}", activityId, e.getMessage(), e);
            return null;
        }
    }

    // 推文创作获取活动列表
    @Override
    public PageResult<ActivityDto> getActivityList(Integer pageNo, Integer pageSize, Integer status) {
        log.info("Dubbo API调用getActivityList，页码: {}, 每页数量: {}, 状态: {}", pageNo, pageSize, status);
        try {
            // 构建查询参数
            ActivityQuery query = new ActivityQuery();
            query.setPageNo(pageNo != null ? pageNo : 1);
            query.setPageSize(pageSize != null ? pageSize : 10);
            query.setStatus(status);

            // 调用service层获取活动列表
            PageResult<ActivityVO> pageResult = activityService.pageList(query);

            // 转换VO列表为DTO列表
            List<ActivityDto> activityDtoList = new ArrayList<>();
            if (pageResult.getObjects() != null && !pageResult.getObjects().isEmpty()) {
                for (ActivityVO activityVO : pageResult.getObjects()) {
                    ActivityDto activityDto = new ActivityDto();
                    BeanUtils.copyProperties(activityVO, activityDto);
                    activityDtoList.add(activityDto);
                }
            }

            // 构建返回结果
            PageResult<ActivityDto> dtoPageResult = new PageResult<>();
            dtoPageResult.setObjects(activityDtoList);
            dtoPageResult.setTotal(pageResult.getTotal());
            dtoPageResult.setPageNo(pageResult.getPageNo());
            dtoPageResult.setPageSize(pageResult.getPageSize());

            return dtoPageResult;
        } catch (BusinessException e) {
            log.error("获取活动列表失败，error: {}", e.getMessage(), e);
            // 返回空的分页结果，避免调用方出现空指针异常
            return new PageResult<>();
        } catch (Exception e) {
            log.error("获取活动列表发生异常，error: {}", e.getMessage(), e);
            // 返回空的分页结果，避免调用方出现空指针异常
            return new PageResult<>();
        }
    }


}